package com.ruoyi.procurementmanagement.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.procurementmanagement.domain.ErpGoods;
import com.ruoyi.procurementmanagement.service.IErpGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procurementmanagement.domain.EsCategory;
import com.ruoyi.procurementmanagement.service.IEsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/procurementmanagement/category")
public class EsCategoryController extends BaseController
{
    @Autowired
    private IEsCategoryService esCategoryService;

    @Autowired
    private IErpGoodsService erpGoodsService;

    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsCategory esCategory)
    {
        List<EsCategory> list = esCategoryService.selectEsCategoryList(esCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsCategory esCategory)
    {
        List<EsCategory> list = esCategoryService.selectEsCategoryList(esCategory);
        ExcelUtil<EsCategory> util = new ExcelUtil<EsCategory>(EsCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(esCategoryService.selectEsCategoryByCategoryId(categoryId));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsCategory esCategory)
    {
        return toAjax(esCategoryService.insertEsCategory(esCategory));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsCategory esCategory)
    {
        return toAjax(esCategoryService.updateEsCategory(esCategory));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(esCategoryService.deleteEsCategoryByCategoryIds(categoryIds));
    }

    /**
     * 根据多个分类ID查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:category:list')")
    @GetMapping("/goodsByCategories")
    public TableDataInfo listGoodsByCategories(
            @RequestParam String categoryIds,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        startPage();
        List<Long> ids = Arrays.stream(categoryIds.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        List<ErpGoods> list = erpGoodsService.selectErpGoodsListByCategoryIds(ids);
        return getDataTable(list);
    }
}
